<?php

namespace App\Http\Controllers\Api;

use App\Schedule;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class ScheduleController extends Controller
{
    public function __construct()
    {
        $this->middleware(function (Request $request,$next){
            $role = User::whereToken($request->token)->pluck('role')->first();
            if (!$role){
                return response()->json('Unauthorized user',401);
            }
            return $next($request);
        });
    }
    public function create(Request $request)
    {
        $schedule = Schedule::create($request->except('token'));
        if ($schedule){
            return response()->json('crate success',200);
        }else{
            return response()->json('Data cannot be processed',422);
        }
    }

    public function delete($id)
    {
        $schedule = Schedule::whereId($id)->delete();
        if ($schedule){
            return response()->json('delete success',200);
        }else{
            return response()->json('Data cannot be deleted',400);
        }
    }
}
